<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jquery ajax</title>
    <link rel="stylesheet" href="js/layui/css/layui.css">
    <script src="js/jquery/jquery.js"></script>
</head>

<body>
<div class="layui-container">
    <p style="margin-top: 5px"></p>
    <br>
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-inline">
            <input type="text" name="a" placeholder="请输入标题" class="layui-input"><span></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="p" placeholder="请输入密码" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn save">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</div>
<script>
    $(function () {
        $('input[name=a]').on('input', function () {
            let t = $(this)
            let m = $(this).val()
            if (m.length < 6) {
                t.next('span').html('太短了，必须6位')
            } else {
                $.ajax({
                    type: 'POST',
                    url: 'http://localhost/api/t2',
                    data: {a: m, action: 'check'},
                    dataType: 'json',
                    cache: false,
                    success: function (e) {
                        t.next('span').html(e.msg)
                    }
                })
            }

        })
    })
</script>
</body>
</html>
